package android.demo.broadcast;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.demo.BaseActivity;
import android.demo.Constants.Action;
import android.demo.Constants.Extra;
import android.demo.R;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class BroadcastActivity extends BaseActivity {

	private Notification n;
	private NotificationManager nm;
	private static final int ID = 1;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		n = new Notification();
		n.icon = R.drawable.icon;
		n.tickerText = "Test Notification";
		n.when = System.currentTimeMillis();
		
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        Button btnSendBroad = new Button(this);
        btnSendBroad.setText("发送广播...");
        btnSendBroad.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setAction(Action.BROADCAST_TEST_RECEIVER);
				intent.putExtra(Extra.BROADCAST_MSG, "testMsg");
				sendBroadcast(intent);
			}
		});
        layout.addView(btnSendBroad, params);
        
        TextView tvNotice = new TextView(this);
        tvNotice.setText("测试notice");
        layout.addView(tvNotice, params);
        
        Button btnSendNotice = new Button(this);
        btnSendNotice.setText("发出通知");
        btnSendNotice.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(BroadcastActivity.this, BroadcastActivity.class);
				PendingIntent pi = PendingIntent.getActivity(BroadcastActivity.this, 0, intent, 0);
				n.setLatestEventInfo(BroadcastActivity.this, "My Title", "My Content", pi);
				nm.notify(ID, n);
			}
		});
        layout.addView(btnSendNotice, params);
        
        Button btnCancelNotice = new Button(this);
        btnCancelNotice.setText("取消通知");
        btnCancelNotice.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				nm.cancel(ID);
			}
		});
        layout.addView(btnCancelNotice, params);

        Button btnSendBroadAndDisplay = new Button(this);
        btnSendBroadAndDisplay.setText("发出广播（DisplayActivity显示）");
        btnSendBroadAndDisplay.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setAction(Action.BROADCAST_DISPLAY_RECEIVER);
				sendBroadcast(intent);
			}
		});
        layout.addView(btnSendBroadAndDisplay, params);
        
        this.setContentView(layout);
	}
}
